import { createAlova } from "alova";
import adapterFetch from "alova/fetch";
import reactHook from "alova/react";
import { message } from "antd";
export const alova = createAlova({
  baseURL: "http://localhost:3000",
  requestAdapter: adapterFetch(),
  beforeRequest: ({ config }) => {
    config.headers.Authorization = `Bearer ${localStorage.getItem("token")}`;
  },
  responded: {
    onSuccess: async (response) => {
      console.log("===", response);
      const result = await response.json();
      if (response.status >= 400) {
        throw new Error(result.message);
        // return Promise.reject(result);
      }

      if (!result.success) {
        // 抛出错误或返回reject状态的Promise实例时，此请求将抛出错误
        throw new Error(result.message);
      }

      // 解析的响应数据将传给method实例的transform钩子函数，这些函数将在后续讲解
      return result;
    },
    onError: (error) => {
      console.log("errpr", error);
      throw new Error(`请求失败：${error.message}`);
    },
    onComplete: () => {},
  },

  statesHook: reactHook,
  timeout: 10000,
});
